<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }

      .contianer {
        width: 600px;
        height: 300px;
        border: 1px solid skyblue;
        margin: 50px auto;
      }

      .menu .tab {
        list-style-type: none;
        float: left;
        cursor: pointer;
        width: 200px;
        line-height: 50px;
        text-align: center;
        border: 1px solid #87ceeb;
        box-sizing: border-box;
      }

      .content .main {
        padding: 20px;
        display: none;
        text-indent: 20px;
        word-wrap: break-word;
      }

      .content .selected {
        display: block;
      }

      .active {
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div class="contianer">
      <div class="menu">
        <ul>
          <li class="tab active">HTML</li>
          <li class="tab">Javascript</li>
          <li class="tab">Vue</li>
        </ul>
      </div>
      <div class="content">
        <div class="main selected">HTML精品教程</div>
        <div class="main">Javascript精品教程</div>
        <div class="main">Vue项目实战</div>
      </div>
    </div>
    <script>
      var liEle = document.getElementsByClassName('tab');
      var mainEle = document.getElementsByClassName('main');
    //   for (var i = 0; i < liEle.length; i++) {
    //     (function (i) {
    //       liEle[i].onclick = function () {
    //         //排他
    //         for (var j = 0; j < liEle.length; j++) {
    //           liEle[j].classList.remove('active');
    //         }
    //         this.classList.add('active');

    //         for (var k = 0; k < mainEle.length; k++) {
    //           mainEle[k].classList.remove('selected');
    //         }
    //         mainEle[i].classList.add('selected');
    //       };
    //     })(i);
    //   }

      //选项卡2.0版本
      for(var i = 0 ; i <  liEle.length ; i++){
        (function(j){
            liEle[j].onclick = function(){
              
                    clearClass(liEle,'active');
                    this.classList.add('active')
                    clearClass(mainEle,'selected');
                    mainEle[j].classList.add('selected')
                }
            
        })(i)
      }
      function clearClass(eles,className){
        for(var i = 0 ; i < eles.length ; i++){
            eles[i].classList.remove(className)
        }
      }

    </script>
  </body>
</html>
